//
//  activityDetailViewController.m
//  UI 作业 - 豆瓣电影
//
//  Created by hesiyao on 15/12/25.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "activityDetailViewController.h"
#import "LoginViewController.h"
#import "UIImageView+WebCache.h"
@interface activityDetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *categoryLabel;
@property (weak, nonatomic) IBOutlet UILabel *address2Label;
@property (weak, nonatomic) IBOutlet UIImageView *titlrPicture;
@property (weak, nonatomic) IBOutlet UIImageView *timeImg;
@property (weak, nonatomic) IBOutlet UIImageView *addressImg;
@property (weak, nonatomic) IBOutlet UIImageView *categoryImg;

@property (weak, nonatomic) IBOutlet UIImageView *address2Img;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;



@end

@implementation activityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view = [[[NSBundle mainBundle] loadNibNamed:@"activityDetailViewController" owner:self options:nil] lastObject ];
    // 上方navigationBar的设置
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share"] style:UIBarButtonItemStylePlain target:self action:@selector(activityLogin)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    // 给各控件赋值
    
    [self addText];
    
    
    
    
}

///给各控件赋值

-(void)addText
{
    self.titleLabel.text = _model.title;
    self.addressLabel.text = _model.address;
    self.categoryLabel.text = _model.category_name;
    self.address2Label.text = _model.name;
    self.detailLabel.text = _model.content;
    self.detailLabel.numberOfLines = 0;
    NSString *beginStr = _model.begin_time;
    NSString *endStr = _model.end_time;
    NSString *newBegin = [beginStr substringFromIndex:5];
    NSString *newEnd = [endStr substringFromIndex:5];
    NSString *str2 = [NSString stringWithFormat:@"%@ -- %@",newBegin, newEnd];
    self.timeLabel.text = str2;
    self.timeImg.image = [UIImage imageNamed:@"icon_date_blue@2x"];
    self.addressImg.image = [UIImage imageNamed:@"icon_sponsor_blue@2x"];
    self.categoryImg.image = [UIImage imageNamed:@"icon_catalog_blue@2x"];
    self.address2Img.image = [UIImage imageNamed:@"icon_spot_blue@2x"];
    [self.titlrPicture sd_setImageWithURL:[NSURL URLWithString:_model.image]];
    
}

/// 右侧分享方法实现
-(void)activityLogin
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    BOOL a = [user objectForKey:@"didLogin"];
    // 判断是不是已经登陆
    if (a) {
        
        // 给我的里面的详情页添加进一个内容 并且确定当前状态为已经收藏
        // 创建活动的文件夹
        NSFileManager *fManage = [NSFileManager defaultManager];
        NSString *ActivityPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"activity"];
        [fManage createDirectoryAtPath:ActivityPath withIntermediateDirectories:YES attributes:nil error:nil];
        // 创建一个当前登陆的人得文件夹
        NSString *userName = [user objectForKey:@"userName"];
        NSString *userPath = [ActivityPath stringByAppendingPathComponent:userName];
        [fManage createDirectoryAtPath:userPath withIntermediateDirectories:YES attributes:nil error:nil];
        
        NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:[userPath stringByAppendingPathComponent:@"activity.plist"]];
        if (arr) {
            for (ActivityModel *m in arr) {
                if ([m.title isEqualToString:_model.title]) {
                    
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该电影已经收藏!" preferredStyle:UIAlertControllerStyleAlert];
                    __weak UIAlertController *art = alert;
                    [self presentViewController:alert animated:YES completion:^{
                        [art dismissViewControllerAnimated:YES completion:^{
                            
                        }];
                    }] ;

                    return;
                }
                
            }
            [arr addObject:_model];
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功!" preferredStyle:UIAlertControllerStyleAlert];
            __weak UIAlertController *art = alert;
            [self presentViewController:alert animated:YES completion:^{
                [art dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }] ;

        }
        // 如果数组并不存在 就创建一个新的数组 把Model加到数组里面 并显示收藏成功
        else
        {
            NSLog(@"111122222");
            arr = [NSMutableArray array];
            [arr addObject:_model];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功!" preferredStyle:UIAlertControllerStyleAlert];
            __weak UIAlertController *art = alert;
            [self presentViewController:alert animated:YES completion:^{
                [art dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }] ;
        }
        // 将数组写进本地
        [NSKeyedArchiver archiveRootObject:arr toFile:[userPath stringByAppendingPathComponent:@"activity.plist"]];
        
        

    }else
    {
     //点击进入的是登陆界面
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:loginVC];
//    loginVC.navigationController.title = @"";
    [self.navigationController presentViewController:navi animated:YES completion:^{
        
    }];
     //点击显示收藏成功 并且加入到收藏里面
     //显示收藏成功
    }
    
    
}

/// 左侧返回方法实现

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
    
    // 设置一个监听者
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"Cell" object:self userInfo:@{@"name" : @"hesiyao"}];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
